package com.un.ebs.memorial.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.common.GroupListInfo;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.memorial.domain.*;
import com.un.ebs.memorial.dto.MemorialAddDto;
import com.un.ebs.memorial.dto.MemorialUpdateDto;
import com.un.ebs.memorial.dto.MemorialVo;
import com.un.ebs.memorial.service.MemorialResourceService;
import com.un.ebs.memorial.service.MemorialService;
import com.un.ebs.sys.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-03-12
 */
@RestController
@RequestMapping("/memorial")
@Api(value = "memorial", tags = "")
public class MemorialController {

    @Autowired
    private MemorialService service;

    @Autowired
    private MemorialResourceService resourceService;

    @Autowired
    private RedisService redisService;

    @PostMapping("create")
    @ApiOperation(value = "创建", notes = "创建")
    public WebResult save(@RequestBody MemorialAddDto req) {
        return WebUtils.success(service.add(req));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public WebResult update(@RequestBody MemorialUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("updateByName")
    @ApiOperation(value = "修改updateByName")
    public WebResult updateByName(@RequestParam String id, @RequestParam String name) {
        service.updateName(id, name);
        return WebUtils.success();
    }

    @PostMapping("updateByNameDesc")
    @ApiOperation(value = "修改updateByNameDesc")
    public WebResult updateByNameDesc(@RequestParam String id, @RequestParam String nameDesc) {
        service.updateNameDesc(id, nameDesc);
        return WebUtils.success();
    }

    @PostMapping("updateByContent")
    @ApiOperation(value = "修改 content")
    public WebResult updateByContent(@RequestParam String id, @RequestParam String content) {
        service.updateContent(id, content);
        return WebUtils.success();
    }

    @PostMapping("updateByLogo/{id}")
    @ApiOperation(value = "修改updateByLogo")
    public WebResult updateByLogo(@PathVariable String id, @RequestParam MultipartFile logo) {
        service.updateLogo(id, logo);
        return WebUtils.success();
    }

    @DeleteMapping("removePhoto")
    @ApiOperation(value = "删除个人相册中的图片")
    public WebResult removePhoto(@RequestParam String id, @RequestParam String key) {
        service.removePhoto(key);
        return WebUtils.success();
    }

    @PostMapping("updateByFrontConver/{id}")
    @ApiOperation(value = "修改封面图")
    public WebResult updateByFrontConver(@PathVariable String id, @RequestParam MultipartFile file) {
        service.updateFrontCover(id, file);
        return WebUtils.success();
    }

    @PostMapping("updateByImage")
    @ApiOperation(value = "修改updateByImage")
    public WebResult updateByImage(@RequestParam String id, @RequestParam String image) {
        service.updateImage(id, image);
        return WebUtils.success();
    }

    @PostMapping("updateByMusic")
    @ApiOperation(value = "修改updateByMusic")
    public WebResult updateByMusic(@RequestParam String id, @RequestParam String music) {
        service.updateMusic(id, music);
        return WebUtils.success();
    }

    @PostMapping("uploadPhoto/{id}")
    @ApiOperation("上传相册")
    public WebResult uploadPhoto(@PathVariable String id, @RequestParam List<MultipartFile> file) {
        service.updatePhoto(id, file);
        return WebUtils.success();
    }

    @PostMapping("uploadLogo/{id}")
    @ApiOperation("上传图标")
    public WebResult uploadLogo(@PathVariable String id, MultipartFile file) {
        return WebUtils.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据主键删除单个 Memorial", notes = "根据主键删除单个 Memorial")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("comment")
    @ApiOperation("评论")
    public WebResult comment(@RequestParam @ApiParam("纪念馆ID") String id, @RequestParam @ApiParam("评论内容") String content) {
        service.recordComment(id, content);
        return WebUtils.success();
    }

    @PostMapping("comment/{recordId}")
    @ApiOperation("评论点赞")
    public WebResult<Integer> commentPraise(@PathVariable String recordId) {
        return WebUtils.success(service.commentPraise(recordId));
    }

    @GetMapping("listComment")
    @ApiOperation("获取评论")
    public WebResult<IPage<MemorialRecordExt>> listComment(@RequestParam @ApiParam("纪念馆ID") String id, @RequestParam int pageIndex, @RequestParam int pageSize) {
        return WebUtils.success(service.queryComment(id, pageIndex, pageSize));
    }

    @PostMapping("record")
    @ApiOperation(value = "用户操作记录", notes = "包括type定义的所有操作类型，例如游客访问、留言、点爱心、送礼物等")
    public WebResult record(@RequestParam @ApiParam("纪念馆ID") String id,
                            @RequestParam @ApiParam("后台定义的操作类别，根据listRecordType获得") String typeId,
                            @RequestParam(required = false) @ApiParam("用于留言") String remark) {
        String key = String.format("%s-%s-%s", id, typeId, SecurityUtils.getUserContent().getId());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        long count = redisService.incAtomicLong(key, 1L,(calendar.getTimeInMillis() - System.currentTimeMillis()) / 60000);
        //redisService.expire(key, (calendar.getTimeInMillis() - System.currentTimeMillis()) / 60000);

        if (count > 1) {
            return WebUtils.success("每天只能点一次哦！");
        }
        service.record(id, typeId, remark);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据主键获取单个 Memorial", notes = "根据主键获取单个 Memorial")
    public WebResult<MemorialVo> getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryViewById(id));
    }

    @GetMapping("photos/{id}")
    @ApiOperation(value = "根据主键获取单个 Memorial")
    public WebResult listPhoto(@PathVariable String id) {
        return WebUtils.success(service.queryPhotos(id));
    }

    @GetMapping("listRecordType")
    @ApiOperation(value = "操作类型")
    public WebResult<List<MemorialRecordType>> listRecordType() {
        return WebUtils.success(service.queryRecordType());
    }

    @GetMapping("listRecordTypeCount")
    @ApiOperation(value = "操作类型统计")
    public WebResult<List<MemorialRecordTypeExt>> listRecordTypeCount(@RequestParam String id, @RequestParam(required = false) String typeId) {
        return WebUtils.success(service.queryRecordTypeCount(id, null));
    }


    @GetMapping("listByDefunctId")
    @ApiOperation(value = "查询纪念馆，根据蜇者ID")
    public WebResult<List<MemorialExt>> listByDefunctId(@RequestParam String id) {
        return WebUtils.success(service.queryViewByDefunctId(id));
    }

    @GetMapping("listMember")
    @ApiOperation(value = "查询访问用户")
    public WebResult<List<MemorialMemberExt>> listMember(@RequestParam String id) {
        return WebUtils.success(service.queryMember(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "分组查询纪念馆", notes = "返回的数据中，包括三类纪念馆：<br>1.top 置顶的;<br>2.hot 热点的; <br> 3.myself 我自己的")
    public WebResult<List<GroupListInfo<Memorial>>> list(
            @RequestParam @ApiParam("其中热点纪念馆的页大小，默认返回第一页，后续调用listByHotPage查询其它页") Integer pageSize) {
        return WebUtils.success(service.queryHomePage(pageSize));
    }

    @GetMapping("listByMyself")
    @ApiOperation(value = "查询自己创建的纪念馆")
    public WebResult<List<MemorialExt>> listByMyself() {
        return WebUtils.success(service.queryByUserId(SecurityUtils.getUserContent().getId()));
    }

    @GetMapping("listByTop")
    @ApiOperation(value = "查询置顶的纪念馆")
    public WebResult<List<MemorialExt>> listByTop() {
        return WebUtils.success(service.query(null, 1, 10));
    }

    @GetMapping("listByHot")
    @ApiOperation(value = "查询最新的纪念馆", notes = "按热度，返回第一页")
    public WebResult<List<MemorialExt>> listByHot(@RequestParam Integer pageSize) {
        return WebUtils.success(service.query(null, 1, pageSize));
    }

    @GetMapping("listByHotPage")
    @ApiOperation(value = "查询最新的纪念馆", notes = "按热度，返回指定页")
    public WebResult<List<MemorialExt>> listByHotPage(@RequestParam(required = false) String name, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.query(name, pageIndex, pageSize));
    }

    @GetMapping("listResource")
    @ApiOperation(value = "查询资源，包括背景图片、背景音乐")
    public WebResult<List<MemorialResource>> listResource() {
        return WebUtils.success(resourceService.queryAll());
    }

    @GetMapping("listRecord/{id}")
    @ApiOperation(value = "查询操作记录")
    public WebResult<IPage<MemorialRecordExt>> listRecord(@PathVariable String id, @RequestParam String typeId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        return WebUtils.success(service.queryRecord(id, typeId, pageIndex, pageSize));
    }

}